import 'dart:math';

import 'package:flutter/cupertino.dart';

class WaveBorderPainter extends CustomPainter {
  Color lineColor;
  Color completeColor;
  Color backgroundColor; // 新增背景颜色参数
  double startPercent;//圆弧转动时 开始的位置
  bool isComplete = false;
  double width;

  WaveBorderPainter(
      {required this.lineColor,
        required this.completeColor,
        required this.startPercent,
        required this.width,
        required this.backgroundColor,
        this.isComplete = false});
  @override
  void paint(Canvas canvas, Size size) {
    Paint line = Paint()
      ..color = lineColor
      ..strokeCap = StrokeCap.round
      ..style = PaintingStyle.stroke
      ..strokeWidth = width;

    Paint complete = Paint()

      ..color = completeColor
      ..strokeCap = StrokeCap.round
      ..style = PaintingStyle.stroke
      ..strokeWidth = width;
    Paint background = Paint()
      ..color = backgroundColor
      ..style = PaintingStyle.fill;
    Offset center = Offset(size.width / 2, size.height / 2); //  坐标中心
    double radius = min(size.width / 2, size.height / 2); //  半径
    Path path = Path();
    path.addOval(Rect.fromCircle(center: center, radius: radius));
    canvas.clipPath(path);
    canvas.drawPath(path, background);
    canvas.drawCircle(
      //  画圆方法
        center,
        radius,
        line);

    double arcAngle = isComplete == true ? startPercent : 2 * pi / 10;

    //画转动的圆弧
    canvas.drawArc(Rect.fromCircle(center: center, radius: radius),
        isComplete == true ? 2 * pi  : startPercent, arcAngle, false, complete);
  }

  @override
  bool shouldRepaint(WaveBorderPainter oldDelegate) {
    return startPercent != oldDelegate.startPercent;
  }
}